#!/bin/sh
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
if [ -z "$LMD_APPS_DIR" ]; then
   echo "Error: Env LMD_APPS_DIR is not defined."
   exit 1
else 
   echo "Env LMD_APPS_DIR: "$LMD_APPS_DIR
fi

export CURRENT_APP_PATH="${LMD_APPS_DIR}/ollama"
mkdir -p "$CURRENT_APP_PATH"
if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: "$CURRENT_APP_PATH
else
    echo "Error, App Dir dose not exist, please retry. "$CURRENT_APP_PATH
    exit 1
fi

source "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/ollama/env"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/download_utils.sh"
echo "lmdprogress:2"

function install_for_mac {
   INSTALLER_URL=$DOWNLOAD_URL_PREFIX/Ollama-darwin.zip
   INSTALLER_FILE_PATH="$CURRENT_APP_PATH/Ollama-darwin-v$APP_VERSION.zip"
   echo "url: "$INSTALLER_URL
   echo "lmdprogress:20"
   download_with_resume "$INSTALLER_FILE_PATH" "$INSTALLER_URL"
   echo "downloaded."
   cd "$CURRENT_APP_PATH"

   echo "lmdprogress:50"

   echo "check "$_MAC_INSTALL_TARGET_FILE_NAME
   # If Ollama.app old file exist, remove it.
   if [ -d "$_MAC_INSTALL_TARGET_FILE_NAME" ];then
      echo "$_MAC_INSTALL_TARGET_FILE_NAME found. Remove it."
      rm -rf "$_MAC_INSTALL_TARGET_FILE_NAME"
   else
      echo "$_MAC_INSTALL_TARGET_FILE_NAME not found."
   fi
   
   echo "lmdprogress:55"
   unzip -o "$INSTALLER_FILE_PATH"

   echo "lmdprogress:75"
   if [ ! -f "$INSTALLER_FILE_PATH" ]; then
      echo "$INSTALLER_FILE_PATH dose not exist."
      exit 1
   fi

   if [ ! -d "$_MAC_INSTALL_PATH" ]; then
      echo "Error: $_MAC_INSTALL_PATH dose not exist."
      exit 1
   fi
   
   # cd "$_MAC_INSTALL_PATH"

   # if cd "$_MAC_INSTALL_PATH"; then
   #    echo "cd: $_MAC_INSTALL_PATH"
   # else
   #    echo "cd err: $_MAC_INSTALL_PATH" >&2
   #    exit 1
   # fi

   # echo "remove old files"
   # rm -rf ./Ollama.app

   # echo "install new files"
   # mv -f "$CURRENT_APP_PATH/Ollama.app" ./

   if [[ "$_MAC_INSTALL_TARGET_FILE_NAME" =~ \.app$ ]] && [[ ${#_MAC_INSTALL_TARGET_FILE_NAME} -gt 5 ]]; then
        echo "$_MAC_INSTALL_TARGET_FILE_NAME is valid"
   else
      echo "$_MAC_INSTALL_TARGET_FILE_NAME is not a valid .app name"
      exit 1
   fi
   echo "copy to $_MAC_INSTALL_PATH"
   osascript <<EOF
    do shell script "cd \"$_MAC_INSTALL_PATH\" && sudo rm -rf \"$_MAC_INSTALL_TARGET_FILE_NAME\" && sudo ditto \"$CURRENT_APP_PATH/$_MAC_INSTALL_TARGET_FILE_NAME\" \"$_MAC_INSTALL_PATH/$_MAC_INSTALL_TARGET_FILE_NAME\"" with administrator privileges
EOF
    exit_code=$?
    if [ $exit_code -ne 0 ]; then
        echo "exit_code: "$exit_code
        exit $exit_code
    fi

   echo "lmdprogress:80"
   
   cd "$CURRENT_APP_PATH"

   OLLAMA_BIN=/Applications/Ollama.app/Contents/Resources/ollama
   if [ -f "$OLLAMA_BIN" ];then
      echo "add ollama command"
      ln -s $OLLAMA_BIN ./ollama
      mv -f ./ollama /usr/local/bin
   fi
   
   echo "lmdprogress:90"
}

function install_for_win {
   INSTALLER_URL=$DOWNLOAD_URL_PREFIX/OllamaSetup.exe
   INSTALLER_FILE_PATH="$CURRENT_APP_PATH/OllamaSetup-v$APP_VERSION.exe"
   echo $INSTALLER_URL
   echo "lmdprogress:20"
   download_with_resume "$INSTALLER_FILE_PATH" $INSTALLER_URL
   # curl --ssl-no-revoke -C - --retry 5 -o "$INSTALLER_FILE_PATH" $INSTALLER_URL
   
   echo "lmdprogress:80"
   "$INSTALLER_FILE_PATH"
   echo "lmdprogress:95"
}

if [ -f /etc/os-release ]; then
   echo "Linux system."
   echo "Not supported."
   exit 1
elif [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
   echo "macOS system."
   install_for_mac
elif current_os_is_windows; then
   echo "Windows system."
   install_for_win
else
   echo "Unknown OS"
fi

echo "lmdprogress:100"
